package uit.shoeshop.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;
import uit.shoeshop.model.DetailModel;
import uit.shoeshop.dto.ProductDTO;
import uit.shoeshop.lib.*;

public class DetailAction extends ActionSupport implements ServletRequestAware,ServletResponseAware{
	private static final long serialVersionUID = 1L;
	private HttpServletRequest request;
	private HttpServletResponse response;

	private String pid;
	private ProductDTO product;
	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}
	
    public String execute() throws Exception {
    	String ret = ERROR;
    	if(getPid() != null && Util.isIntNumber(getPid())){
    		int productId = Integer.parseInt(getPid());
    		DetailModel detail = new DetailModel();
    		try{
	    		setProduct(detail.getProduct(productId));
	    		if(getProduct() != null && getProduct().getTitle() != null){
	    			ret = SUCCESS;
	    		}
    		}catch(Exception e){
    			ret = ERROR;
    		}
    	}
        return ret;//ERROR
    }

	/**
	 * @param pid the pid to set
	 */
	public void setPid(String pid) {
		this.pid = pid;
	}

	/**
	 * @return the pid
	 */
	public String getPid() {
		return pid;
	}

	/**
	 * @param product the product to set
	 */
	public void setProduct(ProductDTO product) {
		this.product = product;
	}

	/**
	 * @return the product
	 */
	public ProductDTO getProduct() {
		return product;
	}

	
}
